package com.seele.tigerwallet.ui.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;

import com.seele.tigerwallet.R;

@SuppressLint("AppCompatCustomView")
public class TransferSeekBar extends SeekBar {

    private OnSeekBarChangeListener mOnSeekBarChangeListener;

    private int mMax;
    private int mMin = 200;

    public TransferSeekBar(Context context) {
        this(context, null);
    }

    public TransferSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs, R.attr.seekBarStyle);
    }

    public TransferSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

    }

    public void setTransferMax(float max) {
        this.mMax = (int)(max * 100);
        super.setMax((mMax - mMin));
    }

    public void setTransferMin(int min) {
        this.mMin = min * 100;
    }

    public float getTransferProgress() {
        int progress = super.getProgress();
        float  floatProgress = (progress + mMin) / 100.0f;
        floatProgress = (float)(Math.round(floatProgress*100))/100;
        return floatProgress;
    }

    public void setTransferProgress(float progress) {
        float prog = progress;
        if (progress < 2) {
            prog = 2;
        }
        prog = prog * 100;
        super.setProgress((int)(prog - mMin));
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
        mOnSeekBarChangeListener = l;
        setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mOnSeekBarChangeListener != null) {
                    float  floatProgress = (progress + mMin) / 100.0f;
                    floatProgress = (float)(Math.round(floatProgress*100))/100;
                    mOnSeekBarChangeListener.onProgressChanged(seekBar, floatProgress, fromUser);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (mOnSeekBarChangeListener != null) {
                    mOnSeekBarChangeListener.onStartTrackingTouch(seekBar);
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mOnSeekBarChangeListener != null) {
                    mOnSeekBarChangeListener.onStopTrackingTouch(seekBar);
                }
            }
        });
    }


    public interface OnSeekBarChangeListener {

        void onProgressChanged(SeekBar seekBar, float progress, boolean fromUser);

        void onStartTrackingTouch(SeekBar seekBar);

        void onStopTrackingTouch(SeekBar seekBar);

    }
}
